//
//  SnapshotController.m
//  Shion
//
//  Created by Chris Karr on 12/28/07.
//  Copyright 2007-2008 Audacious Software. All rights reserved.
//

#import "SnapshotController.h"
#import "Snapshot.h"
#import "Device.h"
#import "DaylightMonitor.h"

@implementation SnapshotController

- (SnapshotController *) init
{
	if (self = [super init])
	{
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(activateByName:) name:ACTIVATE_SNAPSHOT object:nil];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(activateByEvent:) name:DAYLIGHT_UPDATE object:nil];
	}
	
	return self;
}

- (void) activateSnapshot:(Snapshot *) snap
{
	for (NSDictionary * state in snap.deviceStates)
	{
		NSData * address = [state valueForKey:@"address"];
		NSNumber * level = [state valueForKey:@"level"];
		
		for (Device * device in [devices arrangedObjects])
		{
			if ([device.address isEqualToData:address])
				device.visibleStatus.lightLevel = level;
		}
	}
}

- (void) activateByEvent:(NSNotification *) notification
{
	NSString * event = [[notification userInfo] valueForKey:@"Event"];
	
	for (Snapshot * snapshot in [snapshots arrangedObjects])
	{
		if ([snapshot.activateEvent isEqualToString:event])
		{
			NSLog (@"activating %@ for %@", snapshot.name, event);
			[self activateSnapshot:snapshot];
		}
	}
}

- (void) activateByName:(NSNotification *) notification
{
	NSDictionary * userInfo = [notification userInfo];
	
	NSString * name = [userInfo valueForKey:SNAPSHOT_NAME];

	for (Snapshot * snapshot in [snapshots arrangedObjects])
	{
		if ([snapshot.name isEqualToString:name])
			[self activateSnapshot:snapshot];
	}
}
	
- (IBAction) activate:(id) sender
{
	for (Snapshot * snapshot in [snapshots selectedObjects])
		[self activateSnapshot:snapshot];
}

@end
